package cn.wx.scholar.core.service;

import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.wx.scholar.core.entity.AuthorFosMapping;
import cn.wx.scholar.core.entity.ScholarFos;
import cn.wx.common.persistence.base.BaseService;
import cn.wx.common.utils.MD5Util;
import cn.wx.scholar.core.mapper.ScholarFosMapper;

/**
 * @author
 * @since 2018-09-25
 */
@Service
@Transactional
public class ScholarFosService extends BaseService<ScholarFos, ScholarFosMapper> {

	@Autowired
	private RedissonClient redissonClient;

	@Autowired
	private ScholarFosMapper scholarFosMapper;

	/**
	 * 根据领域名称查询领域对象
	 */
	public List<ScholarFos> queryFosByName(Set<String> foses) {
		return scholarFosMapper.queryFosByName(foses);
	}

	
	public List<Long> queryFosIdByName(List<String> foses) {
		return scholarFosMapper.queryFosIdByName(foses);
	}
	
	
	/**
	 * 根据领域名称查询领域的id
	 */
	public Long queryFosId(String fName) {
		/**
		 * 先从redis中获取
		 */

		if (StringUtils.isBlank(fName)) {
			return null;
		}

		final String md5no = MD5Util.MD5(fName);

		/**
		 * 从redis中获取
		 */
		RMapCache<String, Long> map = redissonClient.getMapCache("TXT_AUTHORS_FOS_MAPPING_SAPCE");
		Long pId = map.get(md5no);

		if (pId == null) {
			pId = super.simpleMapper.queryIdByFname(fName);
		}

		map.put(md5no, pId);

		return pId;
	}

	/**
	 * 批量插入专家和领域的关系
	 * 
	 * @param tat
	 * @param list
	 */
	public void modifyBeatchWtihTat(String tat, List<AuthorFosMapping> list) {
		super.simpleMapper.modifyBeatchWtihTat(tat, list);
	}

	/**
	 * 相似领域查询
	 * @param fosId
	 * @return
	 */
	public List<ScholarFos> fosRelative(Long fosId) {
		return super.simpleMapper.fosRelative(fosId);
	}
	
	/**
	 * 子父领域查询
	 * @param fosId
	 * @return
	 */
	public List<ScholarFos> fosPac(String fName) {
		Long fosId = queryFosId(fName);
		List<ScholarFos> result = null;
		result = super.simpleMapper.fosPac(fosId);
		if(result == null || result.size() == 0) {
			result = super.simpleMapper.fosPac1(fosId);
		}
		return result;
	}
	
	/**
	 * 查询子领域
	 * @param fosId
	 * @return
	 */
	public List<ScholarFos> fosChild(String fName) {
		Long fosId = queryFosId(fName);
		List<ScholarFos> result = null;
		result = super.simpleMapper.fosPac(fosId);
		return result;
	}
	
	/**
	 * 子父领域查询
	 * @param fosId
	 * @return
	 */
	public List<ScholarFos> fullView(String fName) {
		Long fosId = queryFosId(fName);
		List<ScholarFos> result = null;
		result = super.simpleMapper.fosPac(fosId);
		
		/**
		 * 如果没有子领域，检查父领域
		 */
		if(result == null || result.size() == 0) {
			
			result = super.simpleMapper.fosPac1(fosId);
			
			/*if(result != null && result.size() >0) {
				Set<Long> pfosIds = new HashSet<>();
				for(ScholarFos sf : result){
					pfosIds.add(sf.getpId());
				}
				
				List<ScholarFosView> kl = super.simpleMapper.fosPacIn(pfosIds);
				
				if(kl != null && kl.size() > 0) {
					Map<Long,List<ScholarFosView>> map = new HashMap<>();
					
					for(ScholarFosView sfl : kl) {
						if(map.get(sfl.getPfosId()) == null) {
							map.put(sfl.getPfosId(), new ArrayList<ScholarFosView>());
						}
						map.get(sfl.getPfosId()).add(sfl);
					}
					
					for(ScholarFos sf1 : result){
						sf1.setChild(map.get(sf1.getpId()));
					}
					
				}
			}*/
		}
		return result;
	}
	
}
